//
//  YWMaterialCirculatiionInfoTableViewCell.m
//  maintain
//
//  Created by 烟雨云渊 on 16/9/22.
//  Copyright © 2016年 ewin. All rights reserved.
//

#import "YWMaterialCirculatiionInfoTableViewCell.h"
#import "YWHandler.h"
#import "YWCommonTool.h"
#import "UIImageView+WebCache.h"
#import "UIColor+CustomColors.h"
#import "Masonry.h"
#import "UIColor+CustomColors.h"
#import "YWMaterialCirculation.h"
#import "YWMacro.h"
#import "YYLabel.h"

@interface YWMaterialCirculatiionInfoTableViewCell()

@property (nonatomic, strong) UIView *topLine;
@property (nonatomic, strong) UIView *bottomLine;

/** 说明 */
@property (nonatomic, strong) YYLabel *noteLabel;
/** 时间 */
@property (nonatomic, strong) YYLabel *timeLabel;
/** 头像 */
@property (nonatomic, strong) UIImageView *headImageView;

@end


@implementation YWMaterialCirculatiionInfoTableViewCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.layer.shouldRasterize = YES;
        self.layer.rasterizationScale = [UIScreen mainScreen].scale;
        [self setUpSubviews];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}

-(void)setIsFirstRow:(BOOL)isFirstRow{
    _isFirstRow = isFirstRow;
}

- (void)setCirculation:(YWMaterialCirculation *)circulation{
    _circulation = circulation;
    YWHandler *creator = _circulation.author;
    NSString* downloadUrl = [YWCommonTool pictureDownloadUrl:creator.headIcon];
    [_headImageView sd_setImageWithURL:[NSURL URLWithString:downloadUrl] placeholderImage:[UIImage imageNamed:@"profile_headimage"]];
    
    NSString* timeStr = nil;
    if (_circulation.createTime) {
        timeStr = [YWCommonTool showTimeWithNormalFormatter:_circulation.createTime / 1000 ];
    }else{
        timeStr = [YWCommonTool showTimeWithNormalFormatter:[[NSDate date] timeIntervalSince1970]];
    }
    _timeLabel.text = timeStr?:@"";
    /*
    YWMaterialCirculationStatusCancel = 0,//撤回申请
    YWMaterialCirculationStatusApply = 1,//发起物料申请
    YWMaterialCirculationStatusStockOut = 2,//领用物料
    YWMaterialCirculationStatusReturned = 3,//归还物料
    YWMaterialCirculationStatusReject = 4//驳回申请
     */

    if (_circulation.type == YWMaterialCirculationStatusApply) {
        NSString * authorName = [NSString stringWithFormat:@"%@",_circulation.author.userName?:@"未知用户"];
        _noteLabel.text = [NSString stringWithFormat:@"%@ 提交了一个物料申请单",authorName];
    }else if (_circulation.type == YWMaterialCirculationStatusStockOut){
        NSString * authorName = [NSString stringWithFormat:@"%@",_circulation.author.userName?:@"未知用户"];
        NSString * receiverName = [NSString stringWithFormat:@"%@",_circulation.recipient.userName?:@"未知用户"];
        _noteLabel.text = [NSString stringWithFormat:@"%@ 将物料发放给 %@",authorName,receiverName];
    }else if (_circulation.type == YWMaterialCirculationStatusCerturnStockOut){
        NSString * authorName = [NSString stringWithFormat:@"%@",_circulation.author.userName?:@"未知用户"];
        _noteLabel.text = [NSString stringWithFormat:@"%@ 已确认接收",authorName];
    }else if (_circulation.type == YWMaterialCirculationStatusReturned){
        NSString * authorName = [NSString stringWithFormat:@"%@",_circulation.author.userName?:@"未知用户"];
        NSString * receiverName = [NSString stringWithFormat:@"%@",_circulation.recipient.userName?:@"未知用户"];
        _noteLabel.text = [NSString stringWithFormat:@"%@ 将物料归还给 %@",authorName,receiverName];
    }else if (_circulation.type == YWMaterialCirculationStatusRejectCerturnStockOut){
        NSString * authorName = [NSString stringWithFormat:@"%@",_circulation.author.userName?:@"未知用户"];
        _noteLabel.text = [NSString stringWithFormat:@"%@ 拒绝确认物料归还  备注: %@",authorName,_circulation.note.length>0?_circulation.note:@"无"];
    }else if (_circulation.type == YWMaterialCirculationStatusCerturnReturned){
        NSString * authorName = [NSString stringWithFormat:@"%@",_circulation.author.userName?:@"未知用户"];
        _noteLabel.text = [NSString stringWithFormat:@"%@ 已确认归还",authorName];
    }else if (_circulation.type == YWMaterialCirculationStatusRejectCerturnStockOut){
        NSString * authorName = [NSString stringWithFormat:@"%@",_circulation.author.userName?:@"未知用户"];
        _noteLabel.text = [NSString stringWithFormat:@"%@ 拒绝确认物料领用  备注: %@",authorName,_circulation.note.length>0?_circulation.note:@"无"];
    }else if (_circulation.type == YWMaterialCirculationStatusCancelApply){
        NSString * authorName = [NSString stringWithFormat:@"%@",_circulation.author.userName?:@"未知用户"];
        _noteLabel.text = [NSString stringWithFormat:@"%@ 撤回物料申请",authorName];
    }else{
        NSString * authorName = [NSString stringWithFormat:@"%@",_circulation.author.userName?:@"未知用户"];
        _noteLabel.text = [NSString stringWithFormat:@"%@ 驳回物料申请",authorName];
    }
//    if (_circulation.status == YWMaterialCirculationStatusApply ) {
//        _noteLabel.text = [NSString stringWithFormat:@"%@发起物料申请",_circulation.person.userName?:@"未知用户"];
//    }else if (_circulation.status == YWMaterialCirculationStatusStockOut){
//        _noteLabel.text = [NSString stringWithFormat:@"%@领用物料",_circulation.person.userName?:@"未知用户"];
//    }else if (_circulation.status == YWMaterialCirculationStatusReturned){
//        _noteLabel.text = [NSString stringWithFormat:@"%@归还物料",_circulation.person.userName?:@"未知用户"];
//    }else if (_circulation.status == YWMaterialCirculationStatusCancel){
//        _noteLabel.text = [NSString stringWithFormat:@"%@撤回物料申请",_circulation.person.userName?:@"未知用户"];
//    }else{
//        _noteLabel.text = [NSString stringWithFormat:@"%@驳回物料申请",_circulation.person.userName?:@"未知用户"];
//    }
    _noteLabel.textColor = _isFirstRow?[UIColor customBlueColor]:[UIColor blackColor];
    
    self.noteLabel.preferredMaxLayoutWidth = self.noteLabel.frame.size.width;
}

- (void) setUpSubviews{
    UIView *superview = self.contentView;
    /**
     *  |
     * 头像
     *  |
     */
    [self.headImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(superview).offset(8);
        make.size.mas_equalTo(CGSizeMake(34, 34));
    }];
    [self.topLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(superview);
        make.bottom.mas_equalTo(_headImageView.mas_top);
    }];
    [self.bottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_headImageView.mas_bottom);
        make.bottom.mas_equalTo(superview);
        make.centerX.mas_equalTo(@[_headImageView, _topLine]);
        make.width.mas_equalTo(@[@1, _topLine]);
    }];
    /**
     *  说明
     *  时间
     */
    [self.noteLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(superview).offset(8);
        make.top.mas_equalTo(_headImageView);
        make.left.mas_equalTo(_headImageView.mas_right).offset(8);
        make.right.mas_equalTo(superview).offset(-8);
    }];
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_noteLabel.mas_bottom).offset(6);
        make.left.and.right.mas_equalTo(_noteLabel);
        make.bottom.mas_equalTo(superview).offset(-8);
    }];
}

//- (void)layoutSubviews{
//    [super layoutSubviews];
//  
//}


- (UIView *)topLine{
    if (!_topLine) {
        _topLine = [UIView new];
        _topLine.backgroundColor = [UIColor customLineColor];
        [self.contentView addSubview:_topLine];
        _topLine.tag = KTopLineTag;
    }
    return _topLine;
}

- (UIImageView *)headImageView{
    if (!_headImageView) {
        UIImageView *imageView = [UIImageView new];
        imageView.clipsToBounds = YES;
        imageView.layer.cornerRadius = 17.0;
        _headImageView = imageView;
        [self.contentView addSubview:_headImageView];
    }
    return _headImageView;
}

- (UIView *)bottomLine{
    if (!_bottomLine) {
        _bottomLine = [UIView new];
        _bottomLine.backgroundColor = [UIColor customLineColor];
        [self.contentView addSubview:_bottomLine];
        _bottomLine.tag = KBottomLineTag;
    }
    return _bottomLine;
}

- (YYLabel *)noteLabel{
    if (!_noteLabel) {
        YYLabel *label = [[YYLabel alloc] init];
        label.font = kContentFont;
        label.textColor = [UIColor customTextGrayBlackColor];
        label.numberOfLines = 0;
        _noteLabel = label;
        [self.contentView addSubview:_noteLabel];
    }
    return _noteLabel;
}

- (YYLabel *)timeLabel{
    if (!_timeLabel) {
        YYLabel *label = [[YYLabel alloc] init];
        label.font = kContentFont;
        label.textColor = [UIColor customTextGrayBlackColor];
        _timeLabel = label;
        [self.contentView addSubview:_timeLabel];
    }
    return _timeLabel;
}

@end
